import java.awt.*;

import javax.swing.*;

import javax.swing.text.*;

import java.text.*;

import java.beans.PropertyChangeListener;

import java.beans.PropertyChangeEvent;

public class jfttDemo extends JPanel

implements PropertyChangeListener {

//Values for the fields

private String textEntry = "10000";

//Labels to identify the fields

private JLabel textEntryLabel;

private JLabel textDisplayLabel;
int   h =1000;
//Strings for the labels

private static String textEntryString = "textEntry: ";

private static String textDisplayString = "textDisplay: ";

//Fields for data entry

private JFormattedTextField textEntryField;

private JFormattedTextField textDisplayField;

//Formats to format and parse numbers

private MaskFormatter textEntryFormat;

private MaskFormatter textDisplayFormat;

public jfttDemo() {

super(new BorderLayout());

setUpFormats();

String textDisplay = textEntry;

//Create the labels.

textEntryLabel = new JLabel(textEntryString);

textDisplayLabel = new JLabel(textDisplayString);

//Create the text fields and set them up.

textEntryField = new JFormattedTextField(textEntryFormat);

textEntryField.setValue(textEntry);

textEntryField.setColumns(25);

textEntryField.setFocusLostBehavior(JFormattedTextField.COMMIT);

textEntryField.addPropertyChangeListener("value", this);

textDisplayField = new JFormattedTextField(textDisplayFormat);

textDisplayField.setValue(textDisplay);

textDisplayField.setColumns(25);

textDisplayField.setEditable(false);

textDisplayField.setForeground(Color.red);

//Tell accessibility tools about label/textfield pairs.

textEntryLabel.setLabelFor(textEntryField);

textDisplayLabel.setLabelFor(textDisplayField);

//Lay out the labels in a panel.

JPanel labelPane = new JPanel(new GridLayout(0,1));

labelPane.add(textEntryLabel);

labelPane.add(textDisplayLabel);

//Layout the text fields in a panel.

JPanel fieldPane = new JPanel(new GridLayout(0,1));

fieldPane.add(textEntryField);

fieldPane.add(textDisplayField);

//Put the panels in this panel, labels on left,

//text fields on right.

setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

add(labelPane, BorderLayout.CENTER);

add(fieldPane, BorderLayout.LINE_END);

}

/** Called when a field's "value" property changes. */

public void propertyChange(PropertyChangeEvent e) {

Object source = e.getSource();

textEntry = textEntryField.getText().trim();

String textDisplay = textEntry;

textDisplayField.setValue(textDisplay);

}

/**

* Create the GUI and show it. For thread safety,

* this method should be invoked from the

* event-dispatching thread.

*/

private static void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("FormattedTextFieldDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

JComponent newContentPane = new jfttDemo();

newContentPane.setOpaque(true); //content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

//Create and set up number formats. These objects also

//parse numbers input by user.

private void setUpFormats() {

try

{

MaskFormatter myFormat = new MaskFormatter();

myFormat.setValidCharacters("*1234567890");

myFormat.setMask("********************");

myFormat.setAllowsInvalid(false);

textEntryFormat = myFormat;

textDisplayFormat = myFormat;

}

catch (ParseException pe)

{

// Do nothing

}

}

}